<template>
  <a-modal
    :width="1000"
    :visible="visible"
    title="围栏管理"
    :body-style="{ backgroundColor: '#F0F2F5', padding: '8px' }"
    :footer="null"
    @update:visible="updateVisible"
  >
    <div class="ele-body">
      <!-- 表格 -->
      <div>
        <a-card :bordered="false">
          <ele-pro-table
            ref="table"
            row-key="areaId"
            :datasource="datasource"
            :columns="columns"
            :where="where"
            v-model:selection="selection"
            :scroll="{ x: 'max-content' }"
          >
            <!-- table上边工具栏 -->
            <template #toolbar>
              <a-space v-permission="['PROPERTY_OXTALL_FENCE_MGR_ADD']">
                <a-button type="primary" @click="openEdit()">
                  <template #icon>
                    <plus-outlined />
                  </template>
                  <span>新建</span>
                </a-button>
              </a-space>
            </template>

            <template #bodyCell="{ column, record }">
              <!-- table操作栏按钮 -->
              <template v-if="column.key === 'action'">
                <a-space>
                  <a v-permission="['PROPERTY_OXTALL_FENCE_MGR_EDIT']" @click="openEdit(record)">修改</a>
                  <a-divider type="vertical" v-permission="['PROPERTY_OXTALL_FENCE_MGR_DEL', 'PROPERTY_OXTALL_FENCE_MGR_EDIT']" />
                  <div v-permission="['PROPERTY_OXTALL_FENCE_MGR_DEL']">
                    <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                      <a class="ele-text-danger">删除</a>
                    </a-popconfirm>
                  </div>
                </a-space>
              </template>
            </template>
          </ele-pro-table>
        </a-card>
      </div>
    </div>
  </a-modal>

  <!-- 编辑弹窗 -->
  <fence-edit v-if="showEdit" v-model:visible="showEdit" :data="current" :oxtId="oxtId" :orgId="orgId" @done="reload" />
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import {FenceApi} from '@/api/property/FenceApi';
import FenceEdit from './oxtall-fence-edit.vue';
import { message } from 'ant-design-vue';

export default {
  name: 'OxtallFence',
  components: {
    PlusOutlined,
    FenceEdit
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 菜单数据
    data: Object
  },
  async mounted() {
  },
  created() {
    this.oxtId = this.data.oxtallId;
    this.orgId = this.data.orgId;
    this.where.oxtId = this.data.oxtallId;
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '围栏编号',
          dataIndex: 'fenceNo'
        },
        {
          title: '围栏名称',
          dataIndex: 'fenceName'
        },
        {
          title: '围栏类型',
          dataIndex: 'fenceType',
          customRender: function (row) {
            if (row.text == 1) {
              return '普通围栏';
            } else if (row.text == 2) {
              return '犊牛栏';
            }
          }
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      oxtId : null,
      orgId : null
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    reset() {
      this.where = {};
      this.where.oxtId = this.data.oxtallId;
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    async remove(row) {
      const result = await FenceApi.delete({ fenceId: row.fenceId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    openEdit(row) {
      this.current = row;
      this.orgId = this.data.orgId;
      this.oxtId = this.data.oxtallId;
      this.showEdit = true;
    },
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return FenceApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
